﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebUI.Utils;

namespace WebUI.Models
{
    public class BaseViewModel
    {
        private string _currentAbsoluteUrl;
        private Boolean _bLogOnPartialView;
        private UserIdentityModel _identity;

        public BaseViewModel()
            : this(true, null)
        {
        }

        public BaseViewModel(object dynParams)
            : this(true, dynParams)
        {
        }

        public string CurrentAbsoluteUrl { get { return _currentAbsoluteUrl; } }
        public Boolean BLogOnPartialView { get { return _bLogOnPartialView; } }
        public UserIdentityModel Identity { get { return _identity; } }

        protected BaseViewModel(Boolean bLogOnPartialView, object dynParams)
        {
            _currentAbsoluteUrl = HttpContext.Current.Request.Url.ToString();
            //if use this if clause here, the benefit is that we get better performance for Http.POST, since we don't need load data again, 
            //but complicated page like page has multiple forms has problem.
            
            //if (HttpContext.Current.Request.RequestType.ToUpper() == "GET")
            {
                _bLogOnPartialView = bLogOnPartialView;
                _identity = ViewModelUtils.CreateUserIdentityModel(bLogOnPartialView);

                SetVars();

                if (dynParams != null)
                {
                    SetVarsWhichDependOnDynamicParams(dynParams);
                    SetPublicVarsAfterBindingDynamicParams();
                }

                
            }
        }

        //set up any variables which depend on passed-in dynamic parameters
        protected virtual void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        { 
        
        }

        //set up any variables which don't depend on any parameters
        protected virtual void SetVars()
        {
        }

        //set up necessary variable, this is public method, it means it can be call outside
        public virtual void SetPublicVarsAfterBindingDynamicParams()
        { 
            
        }
    }

    public class BaseViewModelWithoutLogOnPartial : BaseViewModel
    {
        public BaseViewModelWithoutLogOnPartial()
            : base(false, null)
        { }

        public BaseViewModelWithoutLogOnPartial(object dynParams)
            : base(false, dynParams)
        { }
    }
}